package com.example.ormsqlsplicingframe1.utlis;

import lombok.SneakyThrows;
import sun.util.resources.LocaleData;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class DateUtils {

    private static List<String> list = Arrays.asList(
            "yyyy-MM-dd HH:mm:ss",
            "yyyy-MM-dd HH:mm",
            "yyyy-MM-dd HH",
            "yyyy-MM-dd",
            "yyyy-MM",
            "yyyy");

    @SneakyThrows
    public static Date parse(String value){




        if (value.matches("\\d{13}")) {
            return new Date(Long.decode(value));
        }else {
            for (String s : list) {
                if (s.length() == value.length()){
                    SimpleDateFormat format = new SimpleDateFormat(s);

                    return format.parse(value);
                }
            }

            return null;
        }
    }


    @SneakyThrows
    public static LocalDate LocaleParse(String value){

        ZoneId zoneId = ZoneId.systemDefault();



        if (value.matches("\\d{13}")) {
           return new Date(Long.decode(value)).toInstant().atZone(zoneId).toLocalDate();
        }else {
            for (String s : list) {
                if (s.length() == value.length()){
                    SimpleDateFormat format = new SimpleDateFormat(s);

                   return format.parse(value).toInstant().atZone(zoneId).toLocalDate();
                }
            }
            return null;
        }
    }

}
